package com.evry.spring3sandbox.service;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.evry.spring3sandbox.domain.Product;
import com.evry.spring3sandbox.repository.ProductDao;

@Service("productManager")
@Transactional
public class SimpleProductManager implements ProductManager {

	private static final long serialVersionUID = -7010821624164052421L;
	
	@Autowired
	private ProductDao productDao;

	@Transactional(readOnly = true)
	public List<Product> getProducts() {
        return productDao.getProductList();
    }

    public void increasePrice(BigDecimal percentage) {
        List<Product> products = productDao.getProductList();
        if (products != null) {
            for (Product product : products) {
                double newPrice = product.getPrice().doubleValue() * 
                                    (100 + percentage.intValue())/100;
                product.setPrice(newPrice);
                productDao.saveProduct(product);
            }
        }
    }

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }
   
}
